package weaver.interfaces.ht.wlbmsq;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.xuch.action.DB_Util;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-06-17
 * @Description: ${description}
 * @ModifyLog:
 **/

public class WeaWlbmsqAction extends BaseBean implements Action {
	@Override
	public String execute(RequestInfo requestInfo) {
		RecordSet rs = new RecordSet();
		RecordSet rs1 = new RecordSet();
		BaseBean baseBean = new BaseBean();
		String requestid = requestInfo.getRequestid();
		String curDate = requestInfo.getRequestManager().getCurrentDate().replaceAll("-", "");
		baseBean.writeLog("WeaWlbmsqAction--requestID:" + requestid);
		String tablename = requestInfo.getRequestManager().getBillTableName();// 主表
		try {
			// 主表
			rs.executeQuery("select lcbh,id,lb,gc,xm from " + tablename + " where requestid=?", requestid);
			baseBean.writeLog("select id,lb,gc,lcbh,xm from " + tablename + " where requestid=" + requestid);
			rs.next();
			String mainid = Util.null2String(rs.getString("id"));// 外键
			String xm = Util.null2String(rs.getString("xm"));// 
			String lcbh = Util.null2String(rs.getString("lcbh"));// 
			String lb = Util.null2String(rs.getString("lb"));// 类别 0-备件维修 1-外料其他
			String gc = Util.null2String(rs.getString("gc"));// 工厂
			if (gc.equals("0")) {// 封装
				gc = "1000";
			} else if (gc.equals("1")) {
				gc = "2000";
			}

			ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
			if ("0".equals(lb)) {// 主表类别为备件维修，取明细表1和主表字段
				// 明细1
				rs1.executeQuery(
						"select id,codeqz,mtpy,ms,dw,mg,pg,modelno,aqkc,zwpm,mrpcontroller,valuationclass,dfl,xfl from "
								+ tablename + "_dt1 where mainid=?",
						mainid);
				while (rs1.next()) {
					Map<String, String> parmaMap = new HashMap<>();
					String codeqz = Util.null2String(rs1.getString("codeqz"));// Code前缀
					String mtpy = Util.null2String(rs1.getString("mtpy"));// MTPY
					if ("0".equals(mtpy)) {
						mtpy = "ERSA";
					} else if ("1".equals(mtpy)) {
						mtpy = "ROH";
					} else if ("2".equals(mtpy)) {
						mtpy = "FHMI";
					} else if ("3".equals(mtpy)) {
						mtpy = "HIBE";
					}
					String ms = Util.null2String(rs1.getString("ms"));// 描述
					String dw = Util.null2String(rs1.getString("dw"));// 单位
					String mg = Util.null2String(rs1.getString("mg"));// Matl Group
					String pg = Util.null2String(rs1.getString("pg"));// Purchase Group
					String modelno = Util.null2String(rs1.getString("modelno").toUpperCase());// MODEL NO
					String aqkc = Util.null2String(rs1.getString("aqkc"));// 安全库存
					String zwpm = Util.null2String(rs1.getString("zwpm"));// 中文品名
					String partno = Util.null2String(rs1.getString("partno").toUpperCase());// partno
					String mrpcontroller = Util.null2String(rs1.getString("mrpcontroller"));// mrpcontroller
					String valuationclass = Util.null2String(rs1.getString("valuationclass"));// valuationclass
					
					String iClass2 = Util.null2String(rs1.getString("dfl"));// 大分类
					String iClass3 = Util.null2String(rs1.getString("xfl"));// 小分类
					
					parmaMap.put("WERKS", gc);
					parmaMap.put("ZTXT04", codeqz);
					parmaMap.put("MTART", mtpy);
					parmaMap.put("MAKTX", ms);
					parmaMap.put("MEINS", dw);
					parmaMap.put("MATKL", mg);
					parmaMap.put("EKGRP", pg);
					parmaMap.put("MFRPN", partno);
					parmaMap.put("EISBE", aqkc);
					parmaMap.put("MODEL", modelno);
					parmaMap.put("DISPO", mrpcontroller);
					parmaMap.put("BKLAS", valuationclass);
					baseBean.writeLog("parmaMap:" + parmaMap.toString());
					ZIF_MATERIAL_CREATE create = new ZIF_MATERIAL_CREATE();// 调用函数
					Map resultMap = create.execute(parmaMap, curDate);
					String E_MATNR = (String) resultMap.get("E_MATNR");
					String E_MSG = (String) resultMap.get("E_MSG");
					String E_CHK_TYPE = (String) resultMap.get("E_CHK_TYPE");
					baseBean.writeLog("SAP返回结果——E_MATNR=" + E_MATNR + ";E_MSG=" + E_MSG + ";E_CHK_TYPE=" + E_CHK_TYPE);
					if ("S".equalsIgnoreCase(E_CHK_TYPE)) {
						rs1.executeUpdate("update " + tablename + "_dt1 set sapcdoe='" + E_MATNR + "',emsg='" + E_MSG
								+ "',echktype='" + E_CHK_TYPE + "' where mainid='" + mainid + "'");
						// 发送邮件
						Map<String, String> e = new HashMap<String, String>();
						e.put("ms", ms);
						e.put("xh", partno);
						e.put("dw", dw);
						e.put("gc", codeqz);
						e.put("pg", pg);
						e.put("mtpy", mtpy);
						e.put("mg", mg);
						e.put("mn", modelno);
						e.put("sapcode", E_MATNR);
						list.add(e);

						// SAPCODE返回后，MTPY值为ERSA的或FHMI类型的以FCA或Q开头的物料需要调用以下接口ZRFC_CREATE_COMM_CODE
						if (("ERSA".equals(mtpy)) || ("Q".equals(mtpy.substring(0, 1)))
								|| ("FCA".equals(mtpy.substring(0, 3)))) {
							ZRFC_CREATE_COMM_CODE code = new ZRFC_CREATE_COMM_CODE();
							Map<String, String> map = new HashMap();
							map.put("I_MATNR", E_MATNR);
							map.put("I_PLANT", gc);
							map.put("I_CHNAME", zwpm);
							baseBean.writeLog("map:" + map.toString());
							code.execute(map);
						}
						
						
						Map<String, String> parmaMap_type = new HashMap<>();
						ZMM_UPDATE_MATERIAL_CLASS materialClass = new ZMM_UPDATE_MATERIAL_CLASS();// 调用函数
						parmaMap_type.put("I_MATNR", E_MATNR);
						parmaMap_type.put("I_CLASS2", iClass2);
						parmaMap_type.put("I_CLASS3", iClass3);
						baseBean.writeLog("parmaMap_type:" + parmaMap_type.toString());
						Map resultTYPEMap = materialClass.execute(parmaMap_type);
						String o_result = (String) resultTYPEMap.get("o_result");
						if ("1".equalsIgnoreCase(o_result)){
							baseBean.writeLog("SAP接口ZMM_UPDATE_MATERIAL_CLASS返回结果——o_result=" + o_result + ";成功");
						} else {
							baseBean.writeLog("SAP接口ZMM_UPDATE_MATERIAL_CLASS返回结果——o_result=" + o_result + ";失败");
						}					
					} else {
						requestInfo.getRequestManager().setMessageid("111111");
						requestInfo.getRequestManager().setMessagecontent(E_MSG);
						return Action.FAILURE_AND_CONTINUE;
					}
					
					
					
					
				}
			} else {// 主表类别为外料其他，取明细表2和主表字段
				// 明细2
				rs1.executeQuery(
						"select id,codeqz,mtpy,ms,dw,matlgroup,purchasegroup,modelno,mrpcontroller,valuationclass,zwpm,aqkc from "
								+ tablename + "_dt2 where mainid=?",
						mainid);
				while (rs1.next()) {
					Map<String, String> map1 = new HashMap<>();
					String codeqz = Util.null2String(rs1.getString("codeqz"));// Code前缀
					String mtpy = Util.null2String(rs1.getString("mtpy"));// MTPY
					if ("0".equals(mtpy)) {
						mtpy = "ERSA";
					} else if ("1".equals(mtpy)) {
						mtpy = "ROH";
					} else if ("2".equals(mtpy)) {
						mtpy = "FHMI";
					} else if ("3".equals(mtpy)) {
						mtpy = "HIBE";
					}
					String ms = Util.null2String(rs1.getString("ms"));// 描述
					String dw = Util.null2String(rs1.getString("dw"));// 单位
					String aqkc = Util.null2String(rs1.getString("aqkc"));// aqkc
					String matlgroup = Util.null2String(rs1.getString("matlgroup"));// Matl Group
					String purchasegroup = Util.null2String(rs1.getString("purchasegroup"));// Purchase Group
					String modelno = Util.null2String(rs1.getString("modelno").toUpperCase());// MODEL NO
					String partno = Util.null2String(rs1.getString("partno").toUpperCase());// partno
					String mrpcontroller = Util.null2String(rs1.getString("mrpcontroller"));// MRP CONTROLLER
					String valuationclass = Util.null2String(rs1.getString("valuationclass"));// Valuation Class
					String zwpm = Util.null2String(rs1.getString("zwpm"));// 中文品名
					map1.put("WERKS", gc);
					map1.put("ZTXT04", codeqz);
					map1.put("MTART", mtpy);
					map1.put("MAKTX", ms);
					map1.put("MEINS", dw);
					map1.put("MATKL", matlgroup);
					map1.put("EKGRP", purchasegroup);
					map1.put("MFRPN", partno);
					map1.put("DISPO", mrpcontroller);
					map1.put("EISBE", aqkc);
					map1.put("BKLAS", valuationclass);
					map1.put("MODEL", modelno);

					baseBean.writeLog("map1:" + map1.toString());
					ZIF_MATERIAL_CREATE create = new ZIF_MATERIAL_CREATE();
					Map resultMap = create.execute(map1, curDate);
					String E_MATNR = (String) resultMap.get("E_MATNR");
					String E_MSG = (String) resultMap.get("E_MSG");
					String E_CHK_TYPE = (String) resultMap.get("E_CHK_TYPE");
					baseBean.writeLog("SAP返回结果——E_MATNR=" + E_MATNR + ";E_MSG=" + E_MSG + ";E_CHK_TYPE=" + E_CHK_TYPE);
					if ("S".equalsIgnoreCase(E_CHK_TYPE)) {
						rs1.executeUpdate("update " + tablename + "_dt2 set sapcdoe='" + E_MATNR + "',emsg='" + E_MSG
								+ "',echktype='" + E_CHK_TYPE + "' where mainid='" + mainid + "'");
						// 发送邮件
						Map<String, String> e = new HashMap<String, String>();
						e.put("ms", ms);
						e.put("xh",partno);
						e.put("dw", dw);
						e.put("gc",codeqz);
						e.put("pg", purchasegroup);
						e.put("mtpy", mtpy);
						e.put("mg",matlgroup);
						e.put("mn", modelno);
						e.put("sapcode", E_MATNR);
						list.add(e);
						// SAPCODE返回后，MTPY值为ERSA的或FHMI类型的以FCA或Q开头的物料需要调用以下接口ZRFC_CREATE_COMM_CODE
						if (("ERSA".equals(mtpy)) || ("Q".equals(mtpy.substring(0, 1)))
								|| ("FCA".equals(mtpy.substring(0, 3)))) {
							ZRFC_CREATE_COMM_CODE code = new ZRFC_CREATE_COMM_CODE();
							Map<String, String> map = new HashMap();
							map.put("I_MATNR", E_MATNR);
							map.put("I_PLANT", gc);
							map.put("I_CHNAME", zwpm);
							baseBean.writeLog("map:" + map.toString());
							code.execute(map);
						}
					} else {
						requestInfo.getRequestManager().setMessageid("111111");
						requestInfo.getRequestManager().setMessagecontent("SAP返回报错信息：" + E_MSG);
						return Action.FAILURE_AND_CONTINUE;
					}
				}
			} 
		} catch (Exception e) {
			e.printStackTrace();
			baseBean.writeLog("异常参考:" + e);
			requestInfo.getRequestManager().setMessageid("111111");
			requestInfo.getRequestManager().setMessagecontent(e.getMessage());
			return Action.FAILURE_AND_CONTINUE;
		}
		return Action.SUCCESS;
	}
}
